<?php

/**
 *  @author  Lynn Cho <lynncho@live.com>
 *  @createTime  2016-9-5 13:11:16
 *  @copyright  shewen technology
 *  @descrition  
 */
class Service extends BaseController {

    public function __construct() {
        parent::__construct();
        $this->load->model('ServiceModel');
    }

    public function work() {
        echo 'developing,not now!';
//        $this->load->view('service/work');
    }

    public function chat() {
        $this->load->view('service/chat');
    }

    public function manage() {
        $this->load->view('service/manage');
    }

    public function groupManage() {
        $this->load->view('service/group');
    }

    /**
     * get info list of service
     */
    public function getService() {
        if ($_POST) {
            $account = @htmlspecialchars(trim($this->input->post('account')));
            $name = @htmlspecialchars(trim($this->input->post('name')));
            $group = $this->input->post('group');
//            $online = @htmlspecialchars(trim($this->input->post('online')));
            $stime = @htmlspecialchars(trim($this->input->post('stime')));
            $etime = @htmlspecialchars(trim($this->input->post('etime')));
            $rows = @htmlspecialchars(trim($this->input->post('rows')));
            $page = @htmlspecialchars(trim($this->input->post('page')));
            $re = $this->ServiceModel->getService($account, $name, $group, $stime, $etime, $page, $rows);
            echo json_encode($re);
        }
    }

    public function addService() {
        if ($_POST) {
            //权限
            $array['account'] = @htmlspecialchars(trim($this->input->post('account')));
            $array['pwd'] = @htmlspecialchars(trim($this->input->post('pwd')));
            if (!empty($array['account']) && !empty($array['pwd'])) {
                $array['name'] = @htmlspecialchars(trim($this->input->post('name')));
                $array['phone'] = @htmlspecialchars(trim($this->input->post('phone')));
                $array['email'] = @htmlspecialchars(trim($this->input->post('email')));
                $array['groupid'] = @htmlspecialchars(trim($this->input->post('groupid')));
                $array['offer'] = @htmlspecialchars(trim($this->input->post('offer')));
                if ($this->ServiceModel->addService($array)) {
                    echo true;
                } else {
                    echo json_encode(array('errorMsg' => '插入数据库失败'));
                }
            } else {
                echo json_encode(array('errorMsg' => '用户名或密码不允许为空'));
            }
        }
    }

    public function editService() {
        if ($_POST) {
            //权限
            $array['id'] = @htmlspecialchars(trim($this->input->get('id')));
            $array['phone'] = @htmlspecialchars(trim($this->input->post('phone')));
            $array['email'] = @htmlspecialchars(trim($this->input->post('email')));
            $array['name'] = @htmlspecialchars(trim($this->input->post('name')));
            $array['pwd'] = @htmlspecialchars(trim($this->input->post('pwd')));
            $array['groupid'] = @htmlspecialchars(trim($this->input->post('groupid')));
            $array['offer'] = @htmlspecialchars(trim($this->input->post('offer')));
            if ($this->ServiceModel->editService($array)) {
                echo true;
            } else {
                echo json_encode(array('errorMsg' => '修改管理员资料失败'));
            }
        }
    }

    public function deleteService() {
        if ($_POST) {
            //权限
            $id = @htmlspecialchars(trim($this->input->post('id')));
            if ($this->ServiceModel->deleteService($id)) {
                echo true;
            } else {
                echo json_encode(array('errorMsg' => '删除管理员失败'));
            }
        }
    }

    /**
     * get info list of service group
     */
    public function getGroup() {
        if ($_POST) {
            $account = @htmlspecialchars(trim($this->input->post('account')));
            $name = @htmlspecialchars(trim($this->input->post('name')));
            $rows = @htmlspecialchars(trim($this->input->post('rows')));
            $page = @htmlspecialchars(trim($this->input->post('page')));
            $re = $this->ServiceModel->getGroup($account, $name, $page, $rows);
            echo json_encode($re);
        }
    }

    public function addGroup() {
        if ($_POST) {
            //权限
            $array['account'] = @htmlspecialchars(trim($this->input->post('account')));
            $array['pwd'] = @htmlspecialchars(trim($this->input->post('pwd')));
            if (!empty($array['account']) && !empty($array['pwd'])) {
                $array['name'] = @htmlspecialchars(trim($this->input->post('name')));
                $array['phone'] = @htmlspecialchars(trim($this->input->post('phone')));
                $array['email'] = @htmlspecialchars(trim($this->input->post('email')));
                $array['groupid'] = @htmlspecialchars(trim($this->input->post('groupid')));
                $array['offer'] = @htmlspecialchars(trim($this->input->post('offer')));
                if ($this->ServiceModel->addService($array)) {
                    echo true;
                } else {
                    echo json_encode(array('errorMsg' => '插入数据库失败'));
                }
            } else {
                echo json_encode(array('errorMsg' => '用户名或密码不允许为空'));
            }
        }
    }

    public function editGroup() {
        if ($_POST) {
            //权限
            $array['id'] = @htmlspecialchars(trim($this->input->get('id')));
            $array['phone'] = @htmlspecialchars(trim($this->input->post('phone')));
            $array['email'] = @htmlspecialchars(trim($this->input->post('email')));
            $array['name'] = @htmlspecialchars(trim($this->input->post('name')));
            $array['pwd'] = @htmlspecialchars(trim($this->input->post('pwd')));
            $array['groupid'] = @htmlspecialchars(trim($this->input->post('groupid')));
            $array['offer'] = @htmlspecialchars(trim($this->input->post('offer')));
            if ($this->ServiceModel->editService($array)) {
                echo true;
            } else {
                echo json_encode(array('errorMsg' => '修改管理员资料失败'));
            }
        }
    }

    public function deleteGroup() {
        if ($_POST) {
            //权限
            $id = @htmlspecialchars(trim($this->input->post('id')));
            if ($this->ServiceModel->deleteService($id)) {
                echo true;
            } else {
                echo json_encode(array('errorMsg' => '删除管理员失败'));
            }
        }
    }

    public function getChatLog() {
        if ($_POST) {
            $account = @htmlspecialchars(trim($this->input->post('account')));
            $name = @htmlspecialchars(trim($this->input->post('name')));
            $stime = @htmlspecialchars(trim($this->input->post('stime')));
            $etime = @htmlspecialchars(trim($this->input->post('etime')));
            $rows = @htmlspecialchars(trim($this->input->post('rows')));
            $page = @htmlspecialchars(trim($this->input->post('page')));
            $re = $this->ServiceModel->getChatLog($account, $name, $stime, $etime, $page, $rows);
            echo json_encode($re);
        }
    }

    public function download() {
        if ($_POST) {
            $account = @htmlspecialchars(trim($this->input->post('account')));
            $name = @htmlspecialchars(trim($this->input->post('name')));
            $stime = @htmlspecialchars(trim($this->input->post('stime')));
            $etime = @htmlspecialchars(trim($this->input->post('etime')));
            $re = $this->ServiceModel->getDownloadChat($account, $name, $stime, $etime);
            $record = '';
            $size = 0;
            if (!empty($re)) {
                foreach ($re as $val) {
                    $record = $record . $val['time'] . "  " . "客服：" . $val['service_account'] . "  " . "会话:" . $val['sid'] . "  " . '客户ip：' . $val['ip'] . "  " . "客户临时id:" . $val['cid'] . "  " . '咨询记录：' . $val['content'] . "\r\n";
                    $size = $size + mb_strlen($record, 'utf-8');
                }
            }
            header("Content-type: application/octet-stream");
            header("Accept-Ranges: bytes");
            header("Accept-Length:$size");
            header("Content-Disposition: attachment;filename=chat_log_" . date('YmdHis') . ".txt");
            if (empty($record))
                echo '无记录';
            else
                echo $record;
        }
    }

    public function downloadWithData() {
        $record = $this->input->post('record');
        if (!empty($record)) {
            $recordArr = explode("\\r\\n", $record);
            $record = implode("\r\n", $recordArr);
        }
        $size = mb_strlen($record, 'utf-8');
        header("Content-type: application/octet-stream");
        header("Accept-Ranges: bytes");
        header("Accept-Length:$size");
        header("Content-Disposition: attachment;filename=chat_log_" . date('YmdHis') . ".txt");
        if (empty($record))
            echo '无记录';
        else
            echo $record;
    }

    public function getServiceGroupTree() {
        echo json_encode($this->ServiceModel->getGroupTree());
    }

}
